package com.crm.interceptor;

import com.crm.domain.entity.User;
import com.crm.exception.ReturnViewException;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 登录拦截
 */
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 得到session
        HttpSession session = request.getSession();

        // 得到保存在session中的对象
        User sessionUser = (User) session.getAttribute("session_user");

        // 判断对象是否为空
        if (sessionUser == null) {
            throw new ReturnViewException("page/login", "您还未登录，请先登录!");
        }

        return true;
    }


    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }


    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
